package com.example.administrator.wallet.view.activity.mine.Personal.ReceiptInformation;

import android.app.Activity;
import android.content.Intent;
import android.os.Build;
import android.os.Handler;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;

import com.example.administrator.wallet.R;
import com.example.administrator.wallet.adapter.ReceiptInformationAdapter;
import com.example.administrator.wallet.base.BaseActivity;
import com.example.administrator.wallet.constant.Api;
import com.example.administrator.wallet.intf.IRequestCallback;
import com.example.administrator.wallet.intf.IRequestManager;
import com.example.administrator.wallet.model.bean.GatheringGetListBean;
import com.example.administrator.wallet.model.bean.GetMethodBean;
import com.example.administrator.wallet.utils.DimbgUtils;
import com.example.administrator.wallet.utils.RequestFactory;
import com.example.administrator.wallet.utils.SharedPrefsUtil;
import com.example.administrator.wallet.view.activity.mine.Personal.ReceiptInformation.Alipay.ModifyAlipayActivity;
import com.example.administrator.wallet.view.activity.mine.Personal.ReceiptInformation.BankCards.ModifyBankCardsActivity;
import com.example.administrator.wallet.view.activity.mine.Personal.ReceiptInformation.BankCards.NewBankCardsActivity;
import com.example.administrator.wallet.view.activity.mine.Personal.ReceiptInformation.WeChat.ModifyWeChatActivity;
import com.example.administrator.wallet.view.activity.mine.Personal.ReceiptInformation.WeChat.NewWeChatActivity;
import com.google.gson.Gson;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import butterknife.Bind;

import static com.example.administrator.wallet.R.id.tv_content;

public class ReceiptInformationActivity extends BaseActivity {

    @Bind(R.id.finish_img)
    ImageButton mIb_back;
    @Bind(R.id.ib_add)
    ImageButton mIb_add;
    @Bind(R.id.view_title)
    TextView mV_title;
    @Bind(R.id.lv_contact)
    ListView mLv_contact;
    @Bind(R.id.rl_nohas)
    RelativeLayout mRl_nohas;
    @Bind(R.id.rl_activity_receipt_information)
    RelativeLayout rl_activity_receipt_information;
    @Bind(R.id.nohas_img)
    ImageButton mNohas_img;
    @Bind(R.id.bt_add)
    Button mBt_add;

    private GetMethodBean getMethodBean;
    private List<GetMethodBean.DataBean> datas;
    private SimpleAdapter adapter = null;
    private int code = 0;
    private GatheringGetListBean gatheringGetListBean;
    private List<GatheringGetListBean.DataBean> datas1;
    private GatheringGetListBean.DataBean dataBean;
    private View view;
    private String id = "";//主键id
    private String proceeds_type = "";
    private ReceiptInformationAdapter Readapter;



    @Override
    public void init() {

        datas = new ArrayList<>();
        datas1 = new ArrayList<>();
        //给ListView_main_wordsList注册上下文菜单
        registerForContextMenu(mLv_contact);


        //获取收款信息
        final IRequestManager reqequestManager = RequestFactory.getReqequestManager();
        reqequestManager.httpPost(Api.BaseUrl + Api.gathering_get_list, Api.getGatheringGetList(SharedPrefsUtil.getValue(ReceiptInformationActivity.this, "wallet", "token", "")), new IRequestCallback() {
            @Override
            public void onSuccess(String Response) {
                Gson gson = new Gson();
                gatheringGetListBean = gson.fromJson(Response, GatheringGetListBean.class);
                code = gatheringGetListBean.getCode();
                if (code == 200) {
                    datas1.addAll(gatheringGetListBean.getData());
                    initAdapter();
                    OnClick();

                } else if (code == 501) {
                    mLv_contact.setEmptyView(mRl_nohas);
                }
            }
            @Override
            public void onFailure(String error) {
            }
        });


        mIb_back.setVisibility(View.VISIBLE);
        mV_title.setText(R.string.BankCardManagement);
        //有信息时的创建收款信息
        mIb_add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                IRequestManager reqequestManager = RequestFactory.getReqequestManager();
                reqequestManager.httpPost(Api.BaseUrl + Api.Get_Method, Api.getGet_Method(SharedPrefsUtil.getValue(ReceiptInformationActivity.this,"wallet", "token","")), new IRequestCallback(){
                    @Override
                    public void onSuccess(String Response) {
                        Gson gson = new Gson();
                        getMethodBean = gson.fromJson(Response, GetMethodBean.class);
                        code = getMethodBean.getCode();
                        if (code == 200){

                            initCurrency();

                        }else if(code == 501){
                            Toast.makeText(getApplicationContext(),getMethodBean.getMessage(),Toast.LENGTH_SHORT).show();
                        }else {
                            Toast.makeText(getApplicationContext(),code + getMethodBean.getMessage(),Toast.LENGTH_SHORT).show();
                        }
                    }
                    @Override
                    public void onFailure(String error) {
                    }
                });
            }
        });

        //有信息时标题栏返回键
        mIb_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });
        //无信息时标题栏返回键
        mNohas_img.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });
        //无信息时的创建收款信息
        mBt_add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                IRequestManager reqequestManager = RequestFactory.getReqequestManager();
                reqequestManager.httpPost(Api.BaseUrl + Api.Get_Method, Api.getGet_Method(SharedPrefsUtil.getValue(ReceiptInformationActivity.this,"wallet", "token","")), new IRequestCallback(){
                    @Override
                    public void onSuccess(String Response) {
                        Gson gson = new Gson();
                        getMethodBean = gson.fromJson(Response, GetMethodBean.class);
                        code = getMethodBean.getCode();
                        if (code == 200){

                            initCurrency();

                        }else if(code == 501){
                            Toast.makeText(getApplicationContext(),getMethodBean.getMessage(),Toast.LENGTH_SHORT).show();
                        }else {
                            Toast.makeText(getApplicationContext(),code + getMethodBean.getMessage(),Toast.LENGTH_SHORT).show();
                        }
                    }
                    @Override
                    public void onFailure(String error) {
                    }
                });
            }
        });
    }
    @Override
    public int getLayoutResource() {
        return  R.layout.activity_receipt_information;
    }

    //展示adapter
    private void initAdapter() {
        Readapter = new ReceiptInformationAdapter(this, datas1);
        mLv_contact.setAdapter(Readapter);
    }

    @Override
    public void onResume() {
        super.onResume();
        if (Readapter != null) {
            Readapter.notifyDataSetChanged();
        }
    }
    //点击adapter时
    private void OnClick() {
        Readapter.getClickPst(new ReceiptInformationAdapter.OnClick() {
            @Override
            public void Click(GatheringGetListBean.DataBean dataBean) {
                proceeds_type = dataBean.getIs_qrcode();
                id = dataBean.getId();
                switch (proceeds_type.toString()){
                    case "2":
                        Intent i = new Intent(getApplicationContext(), ModifyAlipayActivity.class);
                        i.putExtra("id",dataBean.getId());
                        startActivity(i);
                        finish();
                        overridePendingTransition(R.anim.next_in_anim, R.anim.next_out_anim);
                        break;
                    case "1":
                        i = new Intent(getApplicationContext(), ModifyWeChatActivity.class);
                        i.putExtra("id",dataBean.getId());
                        startActivity(i);
                        finish();
                        overridePendingTransition(R.anim.next_in_anim, R.anim.next_out_anim);
                        break;
                    case "0":
                        i = new Intent(getApplicationContext(), ModifyBankCardsActivity.class);
                        i.putExtra("id",dataBean.getId());
                        startActivity(i);
                        finish();
                        overridePendingTransition(R.anim.next_in_anim, R.anim.next_out_anim);
                        break;
                }
            }
        });

    }

    //创建收款信息类型列表
    private void initCurrency() {
        view = View.inflate(this, R.layout.popup_currency, null);
        final ListView lv_coin_list = (ListView) view.findViewById(R.id.lv_coin_list);
        final Button bt_cancel = (Button) view.findViewById(R.id.bt_cancel);

        new Handler().post(new Runnable() {
            @Override
            public void run() {
                final PopupWindow window = new PopupWindow(view, RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT, true);
                window.setFocusable(true);
                window.setTouchable(true);
                window.setOutsideTouchable(true);
                window.showAtLocation(rl_activity_receipt_information, Gravity.CENTER, 0, 0);
                Log.e("TAG-----", "OK");
                DimbgUtils.dimBackground(ReceiptInformationActivity.this, 0.5f);
                window.setOnDismissListener(new PopupWindow.OnDismissListener() {
                    @Override
                    public void onDismiss() {
                        DimbgUtils.dimBackground(ReceiptInformationActivity.this, 1.0f);
                    }
                });

                List<GetMethodBean.DataBean> datas=new ArrayList<>();
                datas.addAll(getMethodBean.getData());
                List<HashMap<String,Object>> list = new ArrayList<HashMap<String, Object>>();
                for (int i = 0; i < datas.size(); i++) {
                    HashMap<String,Object> map = new HashMap<String,Object>();
                    GetMethodBean.DataBean data1Bean = datas.get(i);
                    map.put("coin",data1Bean.getName());
                    list.add(map);
                }

                adapter = new SimpleAdapter(ReceiptInformationActivity.this,list, R.layout.item_conlist,new String[]{"coin"},new int[]{tv_content});
                lv_coin_list.setAdapter(adapter);

                lv_coin_list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                      switch (position){
//                          case :
//                              startActivity(new Intent(getApplicationContext(), NewAlipayActivity.class));
//                              finish();
//                              overridePendingTransition(R.anim.next_in_anim, R.anim.next_out_anim);
//                              break;
                          case 0:
                              startActivity(new Intent(getApplicationContext(), NewWeChatActivity.class));
                              finish();
                              overridePendingTransition(R.anim.next_in_anim, R.anim.next_out_anim);
                              break;
                          case 1:
                              SharedPrefsUtil.putValue(ReceiptInformationActivity.this, "wallet", "new_bank_name","");
                              SharedPrefsUtil.putValue(ReceiptInformationActivity.this, "wallet", "new_bank_number","");
                              startActivity(new Intent(getApplicationContext(), NewBankCardsActivity.class));
                              finish();
                              break;
                      }

                        window.dismiss();

                    }
                });
                bt_cancel.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        window.dismiss();
                    }
                });
            }
        });


    }
    @Override
    public void onBackPressed() {
        super.onBackPressed();
        finish();
        overridePendingTransition(R.anim.pre_in_anim, R.anim.pre_out_anim);

    }
    public void setStatuColor() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            Window window = getWindow();
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            window.setStatusBarColor(getResources().getColor(android.R.color.white));
            window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
        }
        setMiuiStatusBarDarkMode(this, true);
        setMiuiStatusBarDarkMode(this, true);
    }
    //设置成白色的背景，字体颜色为黑色。
    public static boolean setMiuiStatusBarDarkMode(Activity activity, boolean darkmode) {
        Class<? extends Window> clazz = activity.getWindow().getClass();
        try {
            int darkModeFlag = 0;
            Class<?> layoutParams = Class.forName("android.view.MiuiWindowManager$LayoutParams");
            Field field = layoutParams.getField("EXTRA_FLAG_STATUS_BAR_DARK_MODE");
            darkModeFlag = field.getInt(layoutParams);
            Method extraFlagField = clazz.getMethod("setExtraFlags", int.class, int.class);
            extraFlagField.invoke(activity.getWindow(), darkmode ? darkModeFlag : 0, darkModeFlag);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    public static boolean setMeizuStatusBarDarkIcon(Activity activity, boolean dark) {
        boolean result = false;
        if (activity != null) {
            try {
                WindowManager.LayoutParams lp = activity.getWindow().getAttributes();
                Field darkFlag = WindowManager.LayoutParams.class
                        .getDeclaredField("MEIZU_FLAG_DARK_STATUS_BAR_ICON");
                Field meizuFlags = WindowManager.LayoutParams.class
                        .getDeclaredField("meizuFlags");
                darkFlag.setAccessible(true);
                meizuFlags.setAccessible(true);
                int bit = darkFlag.getInt(null);
                int value = meizuFlags.getInt(lp);
                if (dark) {
                    value |= bit;
                } else {
                    value &= ~bit;
                }
                meizuFlags.setInt(lp, value);
                activity.getWindow().setAttributes(lp);
                result = true;
            } catch (Exception e) {
            }
        }
        return result;
    }


}
